package com.gopay.bis.service.bank.impl.expay;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.expay.TBankExPayManager;
import com.gopay.bis.service.bank.sms.SmsSupportController;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.YeePayConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.ceb.expay.CebExPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ExPayInteractRemoteService;

/**
 * 测试银行快捷实现
 * 
 * @TBankExPayServiceImpl.java
 * @author WangHongFei
 */
public class TBankExPayServiceImpl implements ExPayInteractRemoteService{

    @Resource(name = "tbankExPayManager")
    private TBankExPayManager tbankExPayManager;
    
    // 短信验证码处理类
    public SmsSupportController smsController;
	
	@Override
	public Result sign(ExPayBaseMessage baseMsg) {
        Result result = new Result(false);
        ExPayBaseMessage message = new ExPayBaseMessage();
        BeanUtils.copyProperties(baseMsg, baseMsg);
        try {
        	message = tbankExPayManager.signAgree(message);
	        result.setDefaultModel(baseMsg);
	        result.setSuccess(true);	        
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		}
        return result;
	}

	@Override
	public Result verify(ExPayBaseMessage baseMsg) {
        return null;
	}

	@Override
	public Result treatyQuery(ExPayBaseMessage baseMsg) {
        return null;
	}

	@Override
	public Result cancel(ExPayBaseMessage baseMsg) {
        Result result = new Result(false);
        ExPayBaseMessage message = new ExPayBaseMessage();
        BeanUtils.copyProperties(baseMsg, baseMsg);
        try {	
        	message = this.tbankExPayManager.cancelAgree(message);
            String tempStr = (String) (message.getExPayHashMap().get("retCd"));
            if (StringUtils.isNotBlank(tempStr) && tempStr.startsWith("S")) {
                result.setSuccess(true);
            }
	        result.setDefaultModel(baseMsg);
	        result.setSuccess(true);	        
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}
        return result;
	}

	@Override
	public Result payment(ExPayBaseMessage baseMsg) {
        Result result = new Result(false);
        ExPayBaseMessage message = new ExPayBaseMessage();
        BeanUtils.copyProperties(baseMsg, baseMsg);
        try {
        	message = this.tbankExPayManager.queryOrder(message);
            String tempStr = (String) (message.getExPayHashMap().get("retCd"));
            if (StringUtils.isNotBlank(tempStr) && tempStr.startsWith("S")) {
                result.setSuccess(true);
            }
	        result.setSuccess(true);	        
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6057);
		}
        return result;
	}

	@Override
	public Result refund(ExPayBaseMessage baseMsg) {
        return null;
	}

	@Override
	public Result queryOrder(ExPayBaseMessage baseMsg) {
        Result result = new Result(false);
        ExPayBaseMessage message = new ExPayBaseMessage();
        BeanUtils.copyProperties(baseMsg, baseMsg);
        try {
        	message = this.tbankExPayManager.queryOrder(message);
            String tempStr = (String) (message.getExPayHashMap().get("retCd"));
            if (StringUtils.isNotBlank(tempStr) && tempStr.startsWith("S")) {
                result.setSuccess(true);
            }
	        result.setSuccess(true);	        
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}
        return result;
	}

	@Override
	public Result certification(ExPayBaseMessage baseMsg) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result sendSms(ExPayBaseMessage baseMsg) {
	    Result result = new Result(false);
        try {
            Map<String, String> smsRes = smsController.sendSMS(baseMsg);
            if (ProcCodeConstants.PROC_CODE_100E6102.equals(smsRes.get("resCode"))) {
                // 短信发送成功
                result.setModel("flag", true);
            } else {
                // 短信发送失败
                result.setModel("flag", false);
            }
            result.setModel("sendCount", smsRes.get("sendCount"));
            result.setModel("retMsg", smsRes.get("retMsg"));
            result.setModel("resCode", smsRes.get("resCode"));
            result.setResultCode(smsRes.get("resCode"));
            result.setSuccess(true);
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6077);
        }
        return result;
	}

	@Override
	public Result checkSms(ExPayBaseMessage baseMsg) {
	    Result result = new Result(false);
        try {
            Map<String, String> smsRes = smsController.checkSMS(baseMsg);
            if (ProcCodeConstants.PROC_CODE_100E6091.equals(smsRes.get("resCode"))) {
                // 验证通过
                result.setModel("flag", true);               
            } else {
                // 验证失败
                result.setModel("flag", false);
                if(!ProcCodeConstants.PROC_CODE_100E6099.equals(smsRes.get("resCode"))){
                    Map<String, String> freezeRes = smsController.freezeCard(baseMsg);
                    if(freezeRes != null)
                        smsRes = freezeRes;
                }
            }
            result.setModel("failCount", smsRes.get("failCount"));
            result.setModel("retMsg", smsRes.get("retMsg"));
            result.setModel("resCode", smsRes.get("resCode"));
            result.setResultCode(smsRes.get("resCode"));
            result.setSuccess(true);
        } catch (Exception ex) {
            BankLogUtils.printServiceImplExceptionLog(ex);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6078);
        }
        return result;
	}

    @Override
    public Result queryCvn(ExPayBaseMessage baseMsg) {
    	Result result = new Result();
        result.setSuccess(true);
        result.setModel("flag", true);
        result.setModel("isQueryCVN", YeePayConstants.YEEPAY_EXPAY_ISQUERYCVN);
        return result;
    }

    public SmsSupportController getSmsController() {
        return smsController;
    }

    public void setSmsController(SmsSupportController smsController) {
        this.smsController = smsController;
    }

    @Override
    public Result queryNeedCert(ExPayBaseMessage baseMsg) {
        // TODO Auto-generated method stub
        return null;
    }
/*	@Override
	public Result refundQuery(ExPayBaseMessage baseMsg) {
		// TODO Auto-generated method stub
		return null;
	}*/

    @Override
    public Result refundBack(ExPayBaseMessage baseMsg) {
        // TODO Auto-generated method stub
        return null;
    }
}
